package com.pzhu.web.servlet;

import com.alibaba.fastjson.JSON;
import com.pzhu.domain.Brand;
import com.pzhu.service.BrandService;
import com.pzhu.service.impl.BrandServiceImpl;
import com.pzhu.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brandStr = request.getReader().readLine();

//        将json字符串转换为brand对象
        Brand brand = JSON.parseObject(brandStr, Brand.class);
        BrandService brandService = new BrandServiceImpl();
        brandService.add(brand);
        response.getWriter().write("success");
    }

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BrandService brandService = new BrandServiceImpl();
        List<Brand> brands = brandService.selectAll();
        String brandsStr = JSON.toJSONString(brands);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(brandsStr);
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brandStr = request.getReader().readLine();
//        将json字符串转换为brand对象
        Brand brand = JSON.parseObject(brandStr, Brand.class);
        BrandService brandService = new BrandServiceImpl();
        brandService.update(brand);
        response.getWriter().write("success");
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brandStr = request.getReader().readLine();
//        将json字符串转换为brand对象
        Brand brand = JSON.parseObject(brandStr, Brand.class);
        BrandService brandService = new BrandServiceImpl();
        brandService.delete(brand.getId());
        response.getWriter().write("success");
    }

    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int[] ids = JSON.parseObject(req.getReader().readLine(), int[].class);
        BrandService brandService = new BrandServiceImpl();
        try {
            brandService.deleteByIds(ids);
            resp.getWriter().write("success");
        } catch (Exception e) {
            resp.getWriter().write("error");
        }
    }
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.接收参数： 当前页码 和 每页分的条目数  url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        BrandService brandService = new BrandServiceImpl();
        //转换成int类型的
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //调用service进行查询
        PageBean<Brand> pageBean = brandService.selectByPage(currentPage, pageSize);

        //2. 转为json
        String jsonString = JSON.toJSONString(pageBean);

        //3. 防止乱码以及写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.接收参数： 当前页码 和 每页分的条目数  url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        BrandService brandService = new BrandServiceImpl();
        //转换成int类型的
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //获取查询条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();//这个就是接收到的参数字符串
        //转为brand类型
        Brand brand = JSON.parseObject(params, Brand.class);

        //调用service进行查询
        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage,pageSize,brand);

        //2. 转为json
        String pageBeanStr = JSON.toJSONString(pageBean);

        //3. 防止乱码以及写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(pageBeanStr);
    }

}
